<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Peeker Quickstart</title>

<style type='text/css' media='all'>@import url('./userguide.css');</style>
<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />

<meta http-equiv='expires' content='-1' />
<meta http-equiv= 'pragma' content='no-cache' />
<meta name='robots' content='all' />

</head>
<body>

<!-- START NAVIGATION -->
<div id="nav"><div id="nav_inner"></div></div>
<div id="nav2"><a name="top">&nbsp;</a></div>
<div id="masthead">
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td><h1>Peeker</h1></td>
<td id="breadcrumb_right"><a href="peeker.html">Peeker</a></td>
</tr>
</table>
</div>
<!-- END NAVIGATION -->


<!-- START BREADCRUMB -->
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td id="breadcrumb">
<a href="https://github.com/sophistry/peeker">Project Home</a> &nbsp;&#8250;&nbsp;
<a href="peeker_toc.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
Quickstart
</td>
</td>
</tr>
</table>
<!-- END BREADCRUMB -->

<br clear="all" />


<!-- START CONTENT -->
<div id="content">


<h1>Peeker Quickstart</h1>

<h2>IMAP examples</h2>
<p>First, make sure your PHP install has the imap extension (run phpinfo() and search for imap).</p>
<p>Note: this Quick Start assumes you are using Peeker as a <a href="http://codeigniter.com">CodeIgniter</a> library.  If you are using Codeigniter, just place all the Peeker files in the application/libraries directory and load the library using CI's $this->load->library('peeker') method.</p> 
<p>However, Peeker <em>can</em> work as a standalone set of classes. Just use the PHP "include" command like this: <code>include('peeker.php');</code> and modify the syntax for the examples below to use regular PHP Object Oriented coding.</p> 
<p>So, instead of <code>$this->load->library('peeker', $config);<br />
// call a method on the CI peeker object <br />
$this->peeker->somefunction();</code> just do 
<code>$peeker = new peeker($config); <br />
// call method on the regular PHP peeker object <br />
$peeker->somefunction();
</code></p>
<p>First, create a new controller function with this code (change login info to match your IMAP login).</p>

<code>
	// this shows basic IMAP, no TLS required<br />
	$config['login']='IMAP_username';<br />
	$config['pass']='IMAP_password';<br />
	$config['host']='imap.domain.com';<br />
	$config['port']='143';<br />
	$config['service_flags'] = '/imap/notls';
	<br />
	<br />
	$this->load->library('peeker', $config);<br />
	<br />
	if ($this->peeker->message_waiting())<br />
	{<br />
		echo 'Message count:' . $this->peeker->get_message_count();<br />
	}<br />
	else<br />
	{<br />
		echo 'No messages waiting.';<br />
	}<br />
	<br />
	$this->peeker->close();<br />
	<br />
	// tell the story of the connection<br />
	print_r($this->peeker->trace());<br />
</code>

<p>If you have unread email in the account, you should see a message with the number of unread emails waiting at the server.</p>

<p>You can connect to any POP or IMAP server. This next config shows how to connect to the gmail.com IMAP server. This example requires that PHP be compiled with SSL. You can verify this with phpinfo().</p>

<p class="important"><strong>Important:</strong>&nbsp; You must <kbd>enable IMAP</kbd> on your gmail account. Go to your gmail account and click 'Settings'. Then click 'Forwarding and POP/IMAP'. Scroll down to 'IMAP Access' and check 'Enable IMAP' and <kbd>click Save Changes</kbd></p>

<code>
	// gmail IMAP<br />
	$config['login']='your_username@gmail.com';<br />
	$config['pass']='your_password';<br />
	$config['host']='imap.gmail.com';<br />
	$config['port']='993';<br />
	$config['service_flags'] = '/imap/ssl/novalidate-cert';
</code>


<h2>POP Examples</h2>

<h3>Basic POP</h3>
<code>
	// Basic POP<br />
	$config['login']='POP_username';<br />
	$config['pass']='POP_password';<br />
	$config['host']='mail.domain.com';<br />
	$config['port']='110';<br />
	$config['service_flags'] = '/pop3/notls';
</code>

<p>This next example shows settings that will connect to gmail's POP server. Just like in the IMAP example, PHP must be configured with SSL for this to work.</p>

<p class="important"><strong>Important:</strong>&nbsp; You must <kbd>enable POP</kbd> on your gmail account. Go to your gmail account and click 'Settings'. Then click 'Forwarding and POP/IMAP'. Scroll down to 'POP Download' and check 'Enable POP for all mail' (if this is a new gmail account with no messages) OR 'Enable POP for mail that arrives from now on' (if this is an existing gmail account with lots of messages) and <kbd>click Save Changes</kbd></p>

<h3>gmail POP</h3>
<code>
	// gmail POP<br />
	$config['login']='your_username@gmail.com';<br />
	$config['pass']='your_password';<br />
	$config['host']='pop.gmail.com';<br />
	$config['port']='995';<br />
	$config['service_flags'] = '/pop3/ssl/novalidate-cert';
</code>

<h2>Some Peeker Functions</h2>

<p>This is a selection of Peeker Class functions - it is not complete, but should get you started exploring.</p>

<h3>$this->peeker->get_message(<var>message_number</var>)</h3>
<p>Get a message in the queue. Returns a message object. The example below uses 1 as the parameter so the code gets the first message waiting. NOTE: Message number index starts at 1. Sending 0 to this method will result in a stream of notices and print a message in the log trace.</p>
<code>$e = $this->peeker->get_message(1);</code>

<h3>$this->peeker->delete_and_expunge(<var>message_number</var>)</h3>
<p>Delete and Expunge the first message in the queue.</p>
<code>$this->peeker->delete_and_expunge(1);</code>

<h3>$this->peeker->close([<var>TRUE</var>])</h3>
<p>Close the connection and (optionally) Expunge any messages that have been marked to delete. (see <dfn>$e->set_delete()</dfn> below)</p>
<code>// sending TRUE as first parameter<br /> 
// tells the server to expunge on close<br />
$this->peeker->close(TRUE);</code>

<h2>Some Message Object Functions</h2>

<p>This is a selection of Peeker Message Object functions (available in the message object) - it is not a complete listing, but it shows enough to let you get a taste of what's possible. A complete listing is available in the <a href="peeker_header.html">Peeker Header</a>, the <a href="peeker_body.html">Peeker Body</a>, the <a href="peeker_parts.html">Peeker Parts</a>, or the <a href="peeker_file.html">Peeker File</a> documentation.</p>

<p class="important"><strong>Important:</strong>&nbsp; Call the following functions using the message object returned by <dfn>$this->peeker->get_message()</dfn>. In these examples it is called <var>$e</var>.</p>

<h3>$e->get_subject()</h3>
<p>Get the subject of the message.</p>
<code>
// get the first message in the queue <br />
$e = $this->peeker->get_message(1);<br />
echo $e->get_subject();<br />
</code>

<h3>$e->get_date()</h3>
<p>Get the date of the message.</p>
<code>
// get the first message in the queue <br />
$e = $this->peeker->get_message(1);<br />
$echo $e->get_date();<br />
</code>

<h3>$e->get_from()</h3>
<p>Get the from string of the message.</p>
<code>
// get the first message in the queue <br />
$e = $this->peeker->get_message(1);<br />
$echo $e->get_from();<br />
</code>

<h3>$e->get_to()</h3>
<p>Get the to string of the message.</p>
<code>
// get the first message in the queue <br />
$e = $this->peeker->get_message(1);<br />
$echo $e->get_to();<br />
</code>

<h3>$e->get_size()</h3>
<p>Get the size (in bytes) of the message.</p>
<code>
// get the first message in the queue <br />
$e = $this->peeker->get_message(1);<br />
$echo $e->get_size();<br />
</code>

<h3>$e->get_header_array()</h3>
<p>Get the entire header (as associative array) of the message.</p>
<code>
// get the first message in the queue <br />
$e = $this->peeker->get_message(1);<br />
$echo $e->get_header_array();<br />
</code>

<h3>$e->set_delete()</h3>
<p>Mark a message in the queue for deletion. Does not automatically delete. You must call <dfn>$this->peeker->close(<var>TRUE</var>)</dfn> and send TRUE as the first parameter.</p>
<code>
// mark the first message for deletion<br />
$e = $this->peeker->get_message(1);<br />
$e->set_delete();<br />
$this->peeker->close(TRUE);<br />
</code>

<h3>$e->get_plain()</h3>
<p>Get the plain part (plain text of the email body) of the message.</p>
<code>
// get the first message in the queue <br />
$e = $this->peeker->get_message(1);<br />
$echo $e->get_plain();<br />
</code>

<h3>$e->get_html()</h3>
<p>Get the HTML part (HTML text of the email body) of the message.</p>
<code>
// get the first message in the queue <br />
$e = $this->peeker->get_message(1);<br />
echo $e->get_html();<br />
</code>

<h3>$e->get_parts_array()</h3>
<p>Get the parts array (all attachments, including PLAIN and HTML attachments) of the message. Each array item is a peeker_file object. The file object has information about each body part and attachment. Dump it to see the structure.</p>
<code>
// get the first message in the queue <br />
$e = $this->peeker->get_message(1);<br />
print_r($e->get_parts_array());<br />
</code>

<p>That is a short summary of some useful functions in Peeker. There are many, many more functions available. Look at the class documentation to learn.</p>

<p>This guide focused on functions; there are other capabilities in the class that were not covered at all in this quickstart: using detectors to write declarative programs, peek at headers (don't disturb read state of message) by setting message  class to peeker_header, extend message object class to create your own detectors.</p>

Requirements:
<ul>
	<li>PHP 5.2+</li>
	<li>IMAP extension</li>
	<li>SSL support (if you want to use a IMAP or POP server over SSL - recommended)</li>
</ul>

</div>
<!-- END CONTENT -->


<div id="footer">
<p>
<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
<a href="peeker_toc.html">User Guide Home</a>
</p>
<p><a href="https://github.com/sophistry/peeker">Peeker</a> &nbsp;&middot;&nbsp; Copyright &#169; 2009-20xx &nbsp;&middot;&nbsp;</p>
</div>

</body>
</html>